package com.bj58.migrate.update.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.bj58.migrate.ScfProxy;
import com.bj58.migrate.update.AbsDataHandler;
import com.bj58.move.Cate2XiaozhiweiMapper;
import com.bj58.sfft.cmc.entity.DispCategory;
import com.bj58.sfft.cmc.entity.DispCategoryExt;
import com.bj58.sfft.cmc.entity.DispLocal;
import com.bj58.sfft.imc.entity.Info;
import com.bj58.sfft.imc.entity.InfoLocal;
import com.bj58.sfft.imc.entity.InfoPara;
import com.bj58.sfft.imc.entity.ParaClass;

public class CateHandleImpl extends AbsDataHandler{

    private Logger logger = Logger.getLogger( CateHandleImpl.class );

    @Override
    public void deal( Info info, String[] args ){

        int cateid = info.getCateID();
        if( info != null && Cate2XiaozhiweiMapper.getXiaozhiCondtion( cateid ) == null ){
            int paramId = Cate2XiaozhiweiMapper.getXiaozhiweiParamId( cateid );// 单元参数id
            if( paramId != -1 && info.getPara() != null ){
                String paramString = String.valueOf( paramId );
                String value = null;
                for( ParaClass para : info.getPara() ){
                    if( para.getKey().equals( paramString ) ){
                        InfoPara infoPara = para.getValue();
                        value = infoPara.getParameterValue();
                        if( value != null ){
                            int thirdCateid = Cate2XiaozhiweiMapper.getThirdCateid( cateid, value );
                            if( thirdCateid != -1 ){
                                try{
                                    List< InfoLocal > infoLocals = info.getLocal();
                                    InfoLocal infoLocal = null;
                                    if( ( infoLocals != null ) && ( infoLocals.size() > 0 ) ){
                                        //set url
                                        String dispListName = null;
                                        infoLocal = infoLocals.get( 0 );
                                        int displocalid = infoLocal.getDispLocalID();
                                        DispLocal entity = ScfProxy.cmc_DispLocal().GetDispLocalByID( displocalid );
                                        if( entity != null ){
                                            //dispListName = entity.getListName();
                                            String fullpath = entity.getFullPath();
                                            if( fullpath != null ){
                                                String[] paths = fullpath.split( "," );
                                                DispLocal parentlocal = ScfProxy.cmc_DispLocal().GetDispLocalByID(
                                                        Integer.parseInt( paths[ 0 ].trim() ) );
                                                if( parentlocal != null ){
                                                    dispListName = parentlocal.getListName();
                                                }
                                            }
                                        }

                                        DispCategoryExt ext = ScfProxy.cmc_DispCategory().GetDefaultDispCateByID( thirdCateid );
                                        int pid = -1;
                                        if( ext != null ){
                                            pid = ext.getPID();
                                        }

                                        String cateListName = null;
                                        if( ( ext != null ) && ( pid != -1 ) ){
                                            DispCategory extp = ScfProxy.cmc_DispCategory().GetDispCategoryByID( pid );
                                            if( extp != null ){
                                                cateListName = extp.getListName();
                                            }
                                        }
                                        if( ( dispListName != null ) && ( cateListName != null ) ){
                                            //set cateid
                                            para.setValue( new InfoPara( paramId, String.valueOf( thirdCateid ) ) );
                                            info.setCateID( thirdCateid );
                                            info.setUrl( String.format( "http://%s.58.com/%s/", dispListName, cateListName ) );
                                            info.Commit();
                                            ScfProxy.imc_InfoProviderWithIndex().UpdateInfo( info );
                                            logger.info(
                                                    "Sucess---" + args[ 2 ] + "------infoid:" + info.getInfoID() + ",cateid:" + cateid + ",value:" +
                                                    value + "---->thiredCateid:" + thirdCateid );
                                        }
                                        else{
                                            logger.error( "Error---" + args[ 2 ] + "------infoid:" + info.getInfoID() + ",cateid:" + cateid +
                                                          ",GetDispListName or GetCateListName NULL" );
                                        }
                                    }
                                    else{
                                        logger.error( "Error---" + args[ 2 ] + "------infoid:" + info.getInfoID() + ",cateid:" + cateid +
                                                      ",List<InfoLocal> is NULL" );
                                    }
                                }
                                catch( Exception e ){
                                    logger.error(
                                            "UpdateCate update info Exception---in " + args[ 2 ] + " ----infoid:" + info.getInfoID() + ",cateid:" +
                                            cateid + ",value:" + value + "-->thiredCateid:" + thirdCateid, e );
                                }
                            }
                        }
                        break;
                    }
                }

            }
        }
    }
}
